﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Admin
{
    public partial class TeacherData : Form
    {
        List<Sys_config> conf_list;
        int grade, m_id;
        public TeacherData()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataTable dt = read_excel();
            if (dt==null)   return;

            List<Student> students = new List<Student>();

            foreach(DataRow dr in dt.Rows)
            {
                Student stu = new Student();
                stu.grade = grade;
                stu.phone = dr["手机号码"].ToString();
                stu.stu_no = dr["学号"].ToString();
                stu.stu_name = dr["姓名"].ToString();
                stu.pwd = "123";
                stu.major_id = m_id;
                students.Add(stu);
            }
            BLL.B_Student b_stu = new BLL.B_Student();
            try
            {
                var res = b_stu.Add(students);
                students = b_stu.GetList(grade, m_id);
                dataGridView1.DataSource = students;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private DataTable read_excel()
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return null;

            string Path = openFileDialog1.FileName;

            System.Data.DataTable dt = null;
            try
            {
                string strConn = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={Path};Extended Properties=Excel 8.0;";

                using (OleDbConnection conn = new OleDbConnection(strConn))
                {
                    conn.Open();
                    System.Data.DataTable sheetDt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    string[] sheet = new string[sheetDt.Rows.Count];
                    for (int i = 0; i < sheetDt.Rows.Count; i++)
                    {
                        sheet[i] = sheetDt.Rows[i]["TABLE_NAME"].ToString();
                    }
                    string strExcel = string.Format("select * from [{0}]", sheet[0]);
                    OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
                    dt = new System.Data.DataTable();
                    myCommand.Fill(dt);
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                return null;
            }
            return dt;
        }
        private void combo_Grade_SelectedIndexChanged(object sender, EventArgs e)
        {
            combo_Major.Items.Clear();
            grade = Convert.ToInt32(combo_Grade.Text);
            List<Sys_config> filteredRecords = conf_list.Where(Sys_config => Sys_config.grade == grade).ToList();

            foreach (var item in filteredRecords)
            {
                combo_Major.Items.Add(item.major_name);
            }
        }

        private void combo_Major_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Sys_config> filteredRecords = conf_list.Where(Sys_config => Sys_config.major_name ==combo_Major.Text).ToList();
            m_id = filteredRecords[0].major_id;


            B_Teacher b_Teacher = new B_Teacher();
            dataGridView2.DataSource = b_Teacher.GetList(grade, m_id);
            dataGridView2.Columns["pwd"].Visible = false;
            dataGridView2.Columns["teacher_uname"].Visible = false;

        }

        private void ImportExcel_Load(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(uiButton3, "选择这些老师成为此年级专业可选的指导老师");
            toolTip1.SetToolTip(uiButton1, "将导师数据导入到导师表");
            B_config b_config = new B_config();
            conf_list = b_config.list_config();
            B_Teacher b_teacher = new B_Teacher();
            dataGridView1.DataSource = b_teacher.GetList();
            dataGridView1.Columns["pwd"].Visible = false;
            dataGridView1.Columns["teacher_uname"].Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<Tutor> tutors = new List<Tutor>();

            foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
            {
                //MessageBox.Show(dr.Cells[0].Value.ToString() + "");
                int teacher_id = (int)dr.Cells[0].Value;
                Tutor t = new Tutor();
                t.teacher_id = teacher_id;
                t.grade= grade;
                t.major_id = m_id;
                t.major_name = combo_Major.Text;
                tutors.Add(t);

            }
            B_Tutor b_Tutor = new B_Tutor();
            b_Tutor.Add(tutors);
            B_Teacher b_Teacher = new B_Teacher();
            dataGridView2.DataSource = b_Teacher.GetList(grade, m_id);
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            List<Tutor> tutors = new List<Tutor>();

            foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
            {
                //MessageBox.Show(dr.Cells[0].Value.ToString() + "");
                int teacher_id = (int)dr.Cells[0].Value;
                Tutor t = new Tutor();
                t.teacher_id = teacher_id;
                t.grade = grade;
                t.major_id = m_id;
                t.major_name = combo_Major.Text;
                tutors.Add(t);

            }
            B_Tutor b_Tutor = new B_Tutor();
            b_Tutor.Add(tutors);
            B_Teacher b_Teacher = new B_Teacher();
            dataGridView2.DataSource = b_Teacher.GetList(grade, m_id);
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            DataTable dt = read_excel();
            if (dt == null) return;

            List<Teacher> teachers = new List<Teacher>();

            foreach (DataRow dr in dt.Rows)
            {
                Teacher t = new Teacher();
                t.teacher_uname = dr["用户名"].ToString();
                t.teacher_name = dr["姓名"].ToString();
                t.depart = dr["工作单位"].ToString();
                t.pwd = "123";
                t.info = dr["信息"].ToString();
                teachers.Add(t);
            }
            BLL.B_Teacher b_teacher = new BLL.B_Teacher();
            try
            {
                //int res = b_teacher.Add(teachers);
                int res = b_teacher.Add_custom(teachers);

                teachers = b_teacher.GetList();
                dataGridView1.DataSource = teachers;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void import_teacher_Click(object sender, EventArgs e)
        {
            DataTable dt = read_excel();
            if (dt == null) return;

            List<Teacher> teachers = new List<Teacher>();

            foreach (DataRow dr in dt.Rows)
            {
                Teacher t = new Teacher();
                t.teacher_uname = dr["用户名"].ToString();
                t.teacher_name = dr["姓名"].ToString();
                t.depart = dr["工作单位"].ToString();
                t.pwd = "123";
                t.info = dr["信息"].ToString();
                teachers.Add(t);
            }
            BLL.B_Teacher b_teacher = new BLL.B_Teacher();
            try
            {
                //int res = b_teacher.Add(teachers);
                int res = b_teacher.Add_custom(teachers);

                teachers = b_teacher.GetList();
                dataGridView1.DataSource = teachers;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
